package com.weakteam.orderonline.dataresource.service;

import com.weakteam.orderonline.dataresource.dao.MenuDAO;
import com.weakteam.orderonline.dataresource.domain.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Nail
 */

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDAO menuDAO;

    @Transactional
    @Override
    public List<Menu> getAll() {
        return menuDAO.getAll();
    }

    @Transactional
    @Override
    public List<Menu> getAllEnabled() {
        return menuDAO.getAllEnabled();
    }

    @Transactional
    @Override
    public Menu getById(Integer id) {
        return menuDAO.getById(id);
    }

    @Transactional
    @Override
    public void save(Menu menu) {
        if (menu.getOrder() == null) {
            Integer maxOrder = this.menuDAO.getMaxOrder();
            menu.setOrder(maxOrder + 1);
        }

        if (menu.getName() == null) {
            menu.setName("new menu");
        }

        if (menu.getEnabled() == null) {
            menu.setEnabled(false);
        }

        menuDAO.save(menu);
    }

    @Transactional
    @Override
    public boolean delete(Integer id) {
        return menuDAO.delete(id);
    }

}
